/**
* @description 修改后的request，返回所有data数据（分页信息）
* @author 杨燕娥
*/

// @ts-ignore
import encryptBase64 from "@/utils/encryptBase64.js"
// @ts-ignore
import encryptMd5 from "@/utils/encryptMd5.js"
//获取全局request中的baseUrl
import { baseUrl } from "@/utils/request" 
const {getAppId} = useStore('app')

export type RequestParam = {
    url: string,
    method: "GET" | "POST" | "DELETE" | "PUT",
    loading?: boolean,
    isFormData?: boolean,
    data?: any,
}

export type ResponseSuccess = {
    code?: number,
    result?: string,
    msg?: string,
    success?:string
}

export function request<T extends RequestParam>(
    {
        url,
        method = 'GET',
        data,
        loading = true,
        isFormData = false
    }: T
): Promise<any> {
    const base64Str = encryptBase64.encode(JSON.stringify(data))
    const md5Str = encryptMd5(base64Str + 123456)
    let _appId = getAppId.value
    const _channelType = '2'
    if (loading) {
        uni.showLoading({
            title: '加载中',
            mask: true
        })
    }

    return new Promise((resolve, reject) => {
        let token = uni.getStorageSync("token");
        uni.request({
            url: baseUrl + url,
            data,
            method,
            header:{
                "Content-Type": isFormData ? "application/x-www-form-urlencoded" : "application/json",
                "Authorization": "Bearer " + token,
            },
            success: (res: UniApp.RequestSuccessCallbackResult) => {
                uni.hideLoading()
                const {
                    code,
                    result,
                    msg,
                    success
                } = res.data as ResponseSuccess
                if (success) {
                    resolve(res.data);
                } else {
                    if (msg) {
                        if (msg.length > 18) {
                            uni.showModal({
                                title: '提示',
                                content: msg,
                                showCancel: false
                            })
                        } else {
                            uni.showToast({
                                title: msg,
                                icon: 'none'
                            })
                        }
                        if(code === 403 || code === 401) {
                            setTimeout(() => {
                                uni.navigateTo({
                                    url: "/commonSubPack/pages/login/index",
                                    complete: (result) => {
                                        console.log(result, "result");
                                    }
                                });
                            }, 1500);
                        }
                    }
                    reject(result);
                }
            },
            fail: (err: UniApp.GeneralCallbackResult) => {
                uni.hideLoading()
                reject(err)
            }
        })
    })
}

